package com.yanqu.road.server.cmd.cookboy;

import com.yanqu.road.entity.activity.cookboy.CookBoyMatch;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatchDish;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatchRank;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cookboy.CrossCookBoyMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;


@Cmd(code = Protocol.C_CROSS_COOK_BOY_MATCH_HISTORY, desc = "请求比赛历史")
public class CrossMatchHistoryCmd extends CrossCommand {

    private long userId;
    private int activityId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CookBoyProto.CookBoyHistoryMatchReqMsg reqMsg = CookBoyProto.CookBoyHistoryMatchReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CookBoyProto.CookBoyHistoryMatchRespMsg.Builder respMsg = CookBoyProto.CookBoyHistoryMatchRespMsg.newBuilder();
        Map<Integer, CookBoyMatch> cookBoyMatchMap = CrossCookBoyMgr.getCookBoyMatchHistory(activityId);
        if(cookBoyMatchMap == null){
            ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
        }
        respMsg.setRet(ret);
        if(ret == 0){
            for(CookBoyMatch cookBoyMatch : cookBoyMatchMap.values()){
                CookBoyProto.CookBoyHistoryMatchTempMsg.Builder historyTemp = CookBoyProto.CookBoyHistoryMatchTempMsg.newBuilder();
                CookBoyMatchRank cookBoyMatchRank = cookBoyMatch.getTopUserRank();
                if(cookBoyMatchRank == null){
                    continue;
                }
                historyTemp.setNo(cookBoyMatch.getNo());
                historyTemp.setUserId(cookBoyMatchRank.getUserId());
                historyTemp.setFoodId(cookBoyMatchRank.getMatchDishList().get(0).getFoodId());
                UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(cookBoyMatchRank.getUserId());
                if(userBaseInfo == null){
                    continue;
                }
                historyTemp.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                historyTemp.setMeJoin(false);
                for(CookBoyMatchDish cookBoyMatchDish : cookBoyMatch.getMatchFoodMap().values()){
                    if(cookBoyMatchDish.getUserId() == userId){
                        historyTemp.setMeJoin(true);
                        break;
                    }
                }
                /*for(CookBoyMatchDish cookBoyMatchDish : cookBoyMatchRank.getMatchDishList()){
                    CookBoyProto.CookBoyFinishFoodTempMsg.Builder dishTemp = CookBoyProto.CookBoyFinishFoodTempMsg.newBuilder();
                    dishTemp.setId(cookBoyMatchDish.getId());
                    dishTemp.setFoodId(cookBoyMatchDish.getFoodId());
                    dishTemp.setFraction(cookBoyMatchDish.getFraction());
                    dishTemp.setDealStatus(cookBoyMatch.getOpenTime() == 0 ? eCookBoyFoodStatusType.OnTable.getValue():eCookBoyFoodStatusType.IsOver.getValue());
                    dishTemp.setNo(cookBoyMatch.getNo());
                    dishTemp.setRank(cookBoyMatch.getDishRank(userId,cookBoyMatchDish.getId()));
                    historyTemp.addFoodList(dishTemp);
                }*/
                respMsg.addHistoryList(historyTemp);
            }
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_COOK_BOY_HISTORY_MATCH, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }

    @Override
    public void sendData() {

    }

}
